#include <iostream>
#include <string>

// 符合责任链设计原理代码
class context
{
public:
    context(std::string name, int day)
    {
        _name = name;
        _day = day;
    }
    std::string _name;
    int _day;
};

class IHandler
{
public:
    virtual ~IHandler() { _next = nullptr; }

    void SetNextHandler(IHandler *next) { _next = next; }

    // 单个处理点处理的逻辑
    bool Handle(const context &ctx)
    {
        if (CanHandle(ctx))
            return HandRequext(ctx);
        else if (GetNextHandler())
            return GetNextHandler()->Handle(ctx);
        return false;
    }

    // 调整责任链逻辑
    static bool handler_leavereq(const context &ctx);

protected:
    virtual bool CanHandle(const context &ctx) { return true; }
    virtual bool HandRequext(const context &ctx) { return true; }
    IHandler *GetNextHandler() { return _next; }

private:
    IHandler *_next;
};

// 具体的处理点
class HandleByProgram : public IHandler
{
protected:
    virtual bool CanHandle(const context &ctx)
    {
        if (ctx._day <= 1)
            return true;
        return false;
    }
    virtual bool HandRequext(const context &ctx) { return true; }
};

class HandleByMainProgram : public IHandler
{
protected:
    virtual bool CanHandle(const context &ctx)
    {
        if (ctx._day <= 3)
            return true;
        return false;
    }
    virtual bool HandRequext(const context &ctx) { return true; }
};

class HandleByBoss : public IHandler
{
protected:
    virtual bool CanHandle(const context &ctx)
    {
        if (ctx._day <= 7)
            return true;
        return false;
    }
    virtual bool HandRequext(const context &ctx) { return true; }
};

bool IHandler::handler_leavereq(const context &ctx)
{
    IHandler *h0 = new HandleByProgram();
    IHandler *h1 = new HandleByMainProgram();
    IHandler *h2 = new HandleByBoss();

    h0->SetNextHandler(h1);
    h1->SetNextHandler(h2);
    return h0->Handle(ctx);
}

int main()
{
    context ctx1("A", 1);
    context ctx2("B", 3);
    context ctx3("C", 7);
    context ctx4("D", 10);

    if (IHandler::handler_leavereq(ctx1))
        std::cout << "a请假成功" << std::endl;

    if (IHandler::handler_leavereq(ctx2))
        std::cout << "b请假成功" << std::endl;

    if (IHandler::handler_leavereq(ctx3))
        std::cout << "c请假成功" << std::endl;

    if (IHandler::handler_leavereq(ctx4))
        std::cout << "d请假成功" << std::endl;
    return 0;
}
